package com.lds.register.center.server.business.appinstance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lds.management.center.common.model.db.OrderBy;
import com.lds.management.center.common.model.db.PageResult;
import com.lds.management.center.common.model.dto.ResultDTO;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseBatchCreateRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseBatchDeleteRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseBatchUpdateRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseCreateRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseDeleteRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseGetByIdRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseGetByIdsRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseGetByKeyRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseListPageRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseListRequest;
import com.lds.register.center.common.model.appinstance.request.AppInstanceBaseUpdateRequest;
import com.lds.register.center.common.model.appinstance.response.AppInstanceBaseBatchCreateResponse;
import com.lds.register.center.common.model.appinstance.response.AppInstanceBaseBatchDeleteResponse;
import com.lds.register.center.common.model.appinstance.response.AppInstanceBaseBatchUpdateResponse;
import com.lds.register.center.common.model.appinstance.response.AppInstanceBaseDeleteResponse;
import com.lds.register.center.common.model.appinstance.response.AppInstanceBaseListResponse;
import com.lds.register.center.common.model.appinstance.response.AppInstanceBaseResponse;
import com.lds.register.center.server.business.appinstance.converter.AppInstanceBaseCreateRequestToEntityConverter;
import com.lds.register.center.server.business.appinstance.converter.AppInstanceBaseGetByKeyRequestToEntityConverter;
import com.lds.register.center.server.business.appinstance.converter.AppInstanceBaseListPageRequestToEntityConverter;
import com.lds.register.center.server.business.appinstance.converter.AppInstanceBaseListRequestToEntityConverter;
import com.lds.register.center.server.business.appinstance.converter.AppInstanceBaseUpdateRequestToEntityConverter;
import com.lds.register.center.server.business.appinstance.converter.AppInstanceToBaseResponseConverter;
import com.lds.register.center.server.business.appinstance.entity.AppInstance;
import com.lds.register.center.server.business.appinstance.mapper.AppInstanceMapper;
import com.lds.register.center.server.business.appinstance.service.AppInstanceBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础服务实现类
 *
 * @author lidongsheng
 * @since 2025-07-16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppInstanceBaseServiceImpl implements AppInstanceBaseService {
    private final AppInstanceMapper appInstanceMapper;
    private final AppInstanceBaseCreateRequestToEntityConverter appInstanceBaseCreateRequestToEntityConverter;
    private final AppInstanceBaseUpdateRequestToEntityConverter appInstanceBaseUpdateRequestToEntityConverter;
    private final AppInstanceBaseListRequestToEntityConverter appInstanceBaseListRequestToEntityConverter;
    private final AppInstanceToBaseResponseConverter appInstanceToBaseResponseConverter;
    private final AppInstanceBaseListPageRequestToEntityConverter appInstanceBaseListPageRequestToEntityConverter;
    private final AppInstanceBaseGetByKeyRequestToEntityConverter appInstanceBaseGetByKeyRequestToEntityConverter;

    @Override
    @Transactional
    public ResultDTO<AppInstanceBaseResponse> createAppInstance(AppInstanceBaseCreateRequest request) {
        try {
            AppInstance appInstance = appInstanceBaseCreateRequestToEntityConverter.convert(request);
            appInstanceMapper.insert(appInstance);
            AppInstanceBaseResponse response = appInstanceToBaseResponseConverter.convert(appInstance);
            return ResultDTO.success(response);
        } catch (DuplicateKeyException e) {
            return ResultDTO.fail("DATA_REPEAT", "数据重复");
        }
    }

    @Transactional
    @Override
    public ResultDTO<AppInstanceBaseBatchCreateResponse> batchCreateAppInstance(
            AppInstanceBaseBatchCreateRequest request) {
        AppInstanceBaseBatchCreateResponse response = AppInstanceBaseBatchCreateResponse.builder()
                .responses(request.getRequests().stream().map(this::createAppInstance).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

    @Transactional
    @Override
    public ResultDTO<AppInstanceBaseDeleteResponse> deleteAppInstance(AppInstanceBaseDeleteRequest request) {
        appInstanceMapper.deleteById(request.getId());

        AppInstanceBaseDeleteResponse response = AppInstanceBaseDeleteResponse.builder()
                .request(request)
                .build();
        return ResultDTO.success(response);
    }

    @Transactional
    @Override
    public ResultDTO<AppInstanceBaseBatchDeleteResponse> batchDeleteAppInstance(
            AppInstanceBaseBatchDeleteRequest request) {
        AppInstanceBaseBatchDeleteResponse response = AppInstanceBaseBatchDeleteResponse.builder()
                .responses(request.getRequests().stream().map(this::deleteAppInstance).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

    @Transactional
    @Override
    public ResultDTO<AppInstanceBaseResponse> updateAppInstance(AppInstanceBaseUpdateRequest request) {
        try {
            AppInstance appInstance = appInstanceBaseUpdateRequestToEntityConverter.convert(request);
            appInstanceMapper.updateById(appInstance);
            AppInstanceBaseResponse response = appInstanceToBaseResponseConverter.convert(appInstance);
            return ResultDTO.success(response);
        } catch (DuplicateKeyException e) {
            return ResultDTO.fail("DATA_REPEAT", "数据重复");
        }
    }

    @Transactional
    @Override
    public ResultDTO<AppInstanceBaseBatchUpdateResponse> batchUpdateAppInstance(
            AppInstanceBaseBatchUpdateRequest request) {
        AppInstanceBaseBatchUpdateResponse response = AppInstanceBaseBatchUpdateResponse.builder()
                .requests(request.getRequests().stream().map(this::updateAppInstance).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

    @Override
    public ResultDTO<AppInstanceBaseResponse> getAppInstanceById(AppInstanceBaseGetByIdRequest request) {
        AppInstance appInstance = appInstanceMapper.selectById(request.getId());
        if (appInstance == null) {
            return ResultDTO.success();
        }

        AppInstanceBaseResponse response = appInstanceToBaseResponseConverter.convert(appInstance);
        return ResultDTO.success(response);
    }

    @Override
    public ResultDTO<AppInstanceBaseListResponse> getAppInstanceByIds(AppInstanceBaseGetByIdsRequest request) {
        List<AppInstance> list = request.getIds().stream().map(id -> appInstanceMapper.selectById(id)).collect(Collectors.toList());
        return ResultDTO.success(AppInstanceBaseListResponse.builder()
                .appInstances(list.stream().map(appInstance -> appInstanceToBaseResponseConverter.convert(appInstance)).collect(Collectors.toList()))
                .build());
    }

    @Override
    public AppInstance getAppInstanceByKey(AppInstanceBaseGetByKeyRequest request) {
        return appInstanceMapper.selectOne(new QueryWrapper<>(appInstanceBaseGetByKeyRequestToEntityConverter.convert(request)));
    }

    @Override
    public ResultDTO<AppInstanceBaseListResponse> listAppInstance(AppInstanceBaseListRequest request) {
        QueryWrapper<AppInstance> wrapper = new QueryWrapper<>(appInstanceBaseListRequestToEntityConverter.convert(request));
        if (StringUtils.isNotEmpty(request.getOrderByKey())) {
            wrapper.orderByDesc(request.getOrderByKey());
        }

        List<AppInstance> appInstances = appInstanceMapper.selectList(wrapper);

        AppInstanceBaseListResponse response = AppInstanceBaseListResponse.builder()
                .appInstances(appInstances.stream().map(appInstanceToBaseResponseConverter::convert).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

    @Override
    public ResultDTO<AppInstanceBaseListResponse> listAllAppInstance(OrderBy orderBy) {
        AppInstanceBaseListRequest appInstanceRequest = AppInstanceBaseListRequest.builder().build();
        appInstanceRequest.setOrderByKey(orderBy.getOrderByKey());
        return listAppInstance(appInstanceRequest);
    }

    @Override
    public ResultDTO<PageResult<AppInstanceBaseResponse>> listAppInstancePage(AppInstanceBaseListPageRequest request) {
        Page<AppInstance> page = new Page<>(request.getPageNum(), request.getPageSize(), true);

        QueryWrapper<AppInstance> wrapper = new QueryWrapper<>(appInstanceBaseListPageRequestToEntityConverter.convert(request));
        if (StringUtils.isNotEmpty(request.getOrderByKey())) {
            if (request.getOrderByDesc() != null && request.getOrderByDesc().booleanValue() == false) {
                wrapper.orderByAsc(request.getOrderByKey());
            } else {
                wrapper.orderByDesc(request.getOrderByKey());
            }
        }

        IPage<AppInstance> pageResult = appInstanceMapper.selectPage(page, wrapper);

        PageResult<AppInstanceBaseResponse> response = new PageResult(
                pageResult.getTotal(), pageResult.getPages(), pageResult.getCurrent(), pageResult.getSize(),
                pageResult.getRecords().stream().map(appInstanceToBaseResponseConverter::convert).collect(Collectors.toList()));
        return ResultDTO.success(response);
    }

    @Override
    public ResultDTO<AppInstanceBaseBatchCreateResponse> batchCreateAppInstanceAsync(AppInstanceBaseBatchCreateRequest request) {
        AppInstanceBaseBatchCreateResponse response = AppInstanceBaseBatchCreateResponse.builder()
                .responses(request.getRequests().parallelStream().map(this::createAppInstance).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

    @Override
    public ResultDTO<AppInstanceBaseBatchDeleteResponse> batchDeleteAppInstanceAsync(AppInstanceBaseBatchDeleteRequest request) {
        AppInstanceBaseBatchDeleteResponse response = AppInstanceBaseBatchDeleteResponse.builder()
                .responses(request.getRequests().parallelStream().map(this::deleteAppInstance).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

    @Override
    public ResultDTO<AppInstanceBaseBatchUpdateResponse> batchUpdateAppInstanceAsync(AppInstanceBaseBatchUpdateRequest request) {
        AppInstanceBaseBatchUpdateResponse response = AppInstanceBaseBatchUpdateResponse.builder()
                .requests(request.getRequests().parallelStream().map(this::updateAppInstance).collect(Collectors.toList()))
                .build();
        return ResultDTO.success(response);
    }

}
